import java.util.regex.Pattern;

/**
 * 十六进制转二进制工具类，支持任意长度输入，处理带空格/无空格格式
 */
public class HexToBinaryConverter {

    // 匹配十六进制字符的正则（仅允许0-9、A-F、a-f，不允许其他字符）
    private static final Pattern HEX_PATTERN = Pattern.compile("^[0-9A-Fa-f\\s]+$");

    /**
     * 将十六进制字符串转为二进制字符串
     * @param hexStr 输入十六进制字符串（支持格式："AA BB CC" 或 "AABBCC"，不区分大小写）
     * @return 对应的二进制字符串（每个十六进制字符对应4位二进制，无空格）
     * @throws IllegalArgumentException 若输入包含非十六进制字符，或输入为空/全空格
     */
    public static String hexToBinary(String hexStr) {
        // 1. 校验输入合法性（非空、非全空格、仅含十六进制字符和空格）
        if (hexStr == null || hexStr.trim().isEmpty()) {
            throw new IllegalArgumentException("输入十六进制字符串不能为空或全空格");
        }
        if (!HEX_PATTERN.matcher(hexStr).matches()) {
            throw new IllegalArgumentException("输入包含非十六进制字符，合法字符：0-9、A-F、a-f、空格");
        }

        // 2. 预处理：去除所有空格，统一转为大写（避免大小写影响）
        String cleanedHex = hexStr.replaceAll("\\s+", "").toUpperCase();

        // 3. 遍历每个十六进制字符，转为4位二进制（补前导零）
        StringBuilder binarySb = new StringBuilder();
        for (int i = 0; i < cleanedHex.length(); i++) {
            char hexChar = cleanedHex.charAt(i);
            // 将单个十六进制字符转为十进制整数（Character.digit支持0-9、A-F）
            int decimalValue = Character.digit(hexChar, 16);
            // 转为4位二进制字符串（%4d：占4位，不足补空格；replace：空格换0）
            String binary4Bit = String.format("%4d", Integer.parseInt(Integer.toBinaryString(decimalValue))).replace(' ', '0');
            // 拼接至结果
            binarySb.append(binary4Bit);
        }

        return binarySb.toString();
    }

    // 测试方法：验证不同格式输入的转换效果
    public static void main(String[] args) {
        // 测试用例1：带空格的十六进制（题目示例格式）
        String hexWithSpace = "AABBCCCCBBAA08021b41D9B2FB2C36353F6100000012b441D9B2FB2C36353F007F000004C261810069690936907A000000C200042553000000007F000000E2099209A700000000DD00000102003C000204008C8CD800000122E3840000249B0000B3000001420000099A099A08A8DC0000016200190DDF0CED21A74F00000182000009000009005780000004E24000000000000000DF000005025A93000000000000DB000001A2C700F31E00F311C1EF000001C20000000000000000EF000001E210000000B9000000800000058209700000000000007F000004C3618100696908779079000000C300042210000000007F000000E308C908D100000000DD00000103003C000204008C8CD700000123E3840000249B0000B300000143000008DE08DE07EFDC0000016300190C620B4209910000000000000000000000000080000004E34000000000000000DF000005035A93000000000000DB000001A3C700E01E00E011C1EF000001C30000000000000000EF000001E310000000BA000000000000000000000000000000007F000004C46181006969096D9079000000C40004266B000000007F000000E409B309B300000000DD00000104003C000204008C8CD700000124E384000024810000B300000144000009D009CC08DEDC0000016400190A700B9C21930000000000000000000000000080000004E44000000000000000DF000005045A93000000000000DB000001A4C700F81E00F811C1EF000001C40000000000000000EF000001E410000000BA000000000000000000000000000000007F000004C5618100696909999079000000C5000426ED000000007F000000E509E509E500000000DD00000105003C000204008C8CD800000125E384000024810000B300000145000009F109FD0907DC0000016500190AE30BF709B80000000000000000000000000080000004E54000000000000000DF000005055A93000000000000DB000001A5C700FC1E00FB11C1EF000001C50000000000000000EF000001E510000000BB000000000000000000000000000000007F000004C66181006969098F907A000000C6000426F2000000007C000000E609F109E800000000DD00000106003C000204008C8CD800000126E3840000248E0000B300000146000009F909F50907DC0000016600190AE70BEF21860000000000000000000000000080000004E64000000000000000DF000005065A93000000000000DC000001A6C700FC1E00FC11C1EF000001C60000000300000000EF000001E610000000B9000000000000000000000000000000007F000004C76181006969098F907A000000C7000426ED0000000070000000E709E809E000000000DD00000107003C000204008C8CD800000127E384000024740000B400000147000009F109F108FADC0000016700190ADF0BE609ED4F00000187000009000009005780000004E74000000000000000DF000005075A93000000000000DD000001A7C700FC1E00FC11C1EF000001C70000000000000000EF000001E710000000B800000080000005870900000000000000E00000052200003F0000003A00BF00000542000C000B00000000DF000005620000000000000000DF000005A20000000000000000EF000005C20000000000000000DF000003200200383838380033BF000003212700383838380033DF0000002024082610471300000183000004C261810069690936907E000000C2000425580000000083000000E209A309AB00000000DE00000102003C000204008C8CD900000122E384000024950000B5000001420000099F099708A8DD0000016200190DDC0CF121A75300000182000009000009005784000004E24000000000000000E0000005025A93000000000000DC000001A2C700F31E00F311C1F0000001C20000000000000000F0000001E211000000300000008400000582097000000000000083000004C361810069690877907D000000C3000422100000000083000000E308C908D500000000DE00000103003C000204008C8CD900000123E384000024950000B500000143000008DE08DE07EBDD0000016300190C620B4209970000000000000000000000000084000004E34000000000000000E0000005035A93000000000000DC000001A3C700E01E00E011C1F0000001C30000000000000000F0000001E311000000310000000000000000000000000000000083000004C46181006969096D907D000000C40004266B0000000083000000E409B309B300000000DE00000104003C000204008C8CD800000124E384000024810000B500000144000009D009CC08E2DD0000016400190A700BA021930000000000000000000000000084000004E44000000000000000E0000005045A93000000000000DC000001A4C700F71E00F811C1F0000001C40000000000000000F0000001E411000000310000000000000000000000000000000083000004C561810069690999907D000000C5000426ED0000000083000000E509E509E500000000DE00000105003C000204008C8CD900000125E384000024810000B500000145000009F509FD090BDD0000016500190AE30BF209B80000000000000000000000000084000004E54000000000000000E0000005055A93000000000000DC000001A5C700FD1E00FB11C1F0000001C50000000000000000F0000001E511000000310000000000000000000000000000000083000004C66181006969098F907E000000C6000426F70000000080000000E609F109E800000000DE00000106003C000204008C8CD900000126E3840000248E0000B500000146000009F909F50907DD0000016600190ADF0BEF21860000000000000000000000000084000004E64000000000000000E0000005065A93000000000000DD000001A6C700FB1E00FC11C1F0000001C60000000300000000F0000001E611000000310000000000000000000000000000000083000004C76181006969098F907E000000C7000426ED0000000074000000E709ED09E000000000DE00000107003C000204008C8CD900000127E3840000247A0000B600000147000009F509F508FADD0000016700190AE30BEA09E65300000187000009000009005784000004E74000000000000000E0000005075A93000000000000DE000001A7C700FC1E00FB11C1F0000001C70000000000000000F0000001E7110000003000000084000005870900000000000000E10000052200003F0000003A00C100000542000C000B00000000E0000005620000000000000000E0000005A20000000000000000F0000005C20000000000000000E0000003200200383838380033C1000003212700383838380033E00000002024082610471300000287000004C2618100696909369082000000C2000425530000000087000000E2099A09AB00000000DF00000102003C000204008C8CDA00000122E3840000249B0000B7000001420000099A099A08A8DE0000016200190DDC0CF121A75700000182000009000009005788000004E24000000000000000E1000005025A93000000000000DD000001A2C700F21E00F311C1F0000001C20000000000000000F0000001E211000000300000008800000582097000000000000087000004C3618100696908779081000000C3000422100000000087000000E308C508D500000000DF00000103003C000204008C8CDA00000123E3840000249B0000B700000143000008D908D907EBDE0000016300190C620B4209910000000000000000000000000088000004E34000000000000000E1000005035A93000000000000DD000001A3C700E01E00E011C1F0000001C30000000000000000F0000001E311000000310000000000000000000000000000000087000004C46181006969096D9081000000C40004266B0000000087000000E409B709B700000000DF00000104003C000204008C8CD900000124E384000024810000B700000144000009D009CC08E2DE0000016400190A700BA021930000000000000000000000000088000004E44000000000000000E1000005045A93000000000000DD000001A4C700F71E00F811C1F0000001C40000000000000000F0000001E411000000310000000000000000000000000000000087000004C5618100696909999081000000C5000426ED0000000087000000E509E809E800000000DF00000105003C000204008C8CDA00000125E3840000247A0000B700000145000009F109FD0907DE0000016500190AE30BF209B80000000000000000000000000088000004E54000000000000000E1000005055A93000000000000DD000001A5C700FC1E00FB11C1F0000001C50000000000000000F0000001E511000000310000000000000000000000000000000087000004C66181006969098F9082000000C6000426F70000000084000000E609F109E800000000DF00000106003C000204008C8CDA00000126E384000024950000B700000146000009F909F508FFDE0000016600190AE70BEF21860000000000000000000000000088000004E64000000000000000E1000005065A93000000000000DE000001A6C700FC1E00FC11C1F0000001C60000000300000000F0000001E611000000310000000000000000000000000000000087000004C76181006969098F9082000000C7000426ED0000000078000000E709ED09E000000000DF00000107003C000204008C8CDA00000127E3840000247A0000B800000147000009F509F508FADE0000016700190ADF0BEF09E65700000187000009000009005788000004E74000000000000000E1000005075A93000000000000DF000001A7C700FB1E00FB11C1F0000001C70000000000000000F0000001E7110000003000000088000005870900000000000000E20000052200003F0000003A00C300000542000C000B00000000E1000005620000000000000000E1000005A20000000000000000F0000005C20000000000000000E1000003200200383838380033C3000003212700383838380033E1000000202408261047130000038B000004C2618100696909369086000000C200042553000000008B000000E2099A09A700000000E000000102003C000204008C8CDB00000122E3840000249B0000B9000001420000099A099A08A8DF0000016200190DDC0CF121A75B0000018200000900000900578C000004E24000000000000000E2000005025A93000000000000DE000001A2C700F31E00F311C1F1000001C20000000000000000F1000001E212000001AF0000008C0000058209700000000000008B000004C3618100696908779085000000C300042210000000008B000000E308C508D500000000E000000103003C000204008C8CDB00000123E384000024950000B900000143000008DE08DE07EFDF0000016300190C620B460991000000000000000000000000008C000004E34000000000000000E2000005035A93000000000000DE000001A3C700E01E00E011C1F1000001C30000000000000000F1000001E31200000186000000000000000000000000000000008B000004C46181006969096D9085000000C40004266B000000008B000000E409B309B300000000E000000104003C000204008C8CDA00000124E384000024810000B900000144000009D009CC08E2DF0000016400190A700B9C2193000000000000000000000000008C000004E44000000000000000E2000005045A93000000000000DE000001A4C700F81E00F811C1F1000001C40000000000000000F1000001E41200000189000000000000000000000000000000008B000004C5618100696909999085000000C5000426ED000000008B000000E509E509E000000000E000000105003C000204008C8CDB00000125E384000024810000B900000145000009F109FD0907DF0000016500190AE30BF709B8000000000000000000000000008C000004E54000000000000000E2000005055A93000000000000DE000001A5C700FD1E00FC11C1F1000001C50000000000000000F1000001E51200000181000000000000000000000000000000008B000004C66181006969098F9086000000C6000426F20000000088000000E609ED09E800000000E000000106003C000204008C8CDB00000126E3840000248E0000B900000146000009F909F508FFDF0000016600190AE30BEF2180000000000000000000000000008C000004E64000000000000000E2000005065A93000000000000DF000001A6C700FB1E00FC11C1F1000001C60000000300000000F1000001E61200000188000000000000000000000000000000008B000004C76181006969098F9086000000C7000426ED000000007C000000E709E809E000000000E000000107003C000204008C8CDB00000127E384000024740000BA00000147000009F509F508FADF0000016700190ADF0BEA09E65B0000018700000900000900578C000004E74000000000000000E2000005075A93000000000000E0000001A7C700FB1E00FB11C1F1000001C70000000000000000F1000001E712000001AF0000008C000005870900000000000000E30000052200003F0000003A00C500000542000C000B00000000E2000005620000000000000000E2000005A20000000000000000F1000005C20000000000000000E2000003200200383838380033C5000003212700383838380033E2000000202408261047130000FF55AAAA55FF";
        String binaryWithSpace = hexToBinary(hexWithSpace);
        System.out.println("输入1：" + hexWithSpace);
        System.out.println("输出1：" + binaryWithSpace + "\n");
    }
}
    